// pages/order/insuranceOrder/insuranceOrder.js
const app = getApp();
const util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ orderId: options.orderId });
    this.getData();
  },
  /**
   * 获取数据
   */
  getData() {
    app.request('/Order/OrderDetailForInsurance', 'GET', { orderId: this.data.orderId }, res => {
      this.setData({
        carPhoto: res.quotationCarModel.carPhoto,
        licencePlate: res.quotationCarModel.licencePlate,
        price: res.orderDetail.price,
        discount: res.orderDetail.discount/10,
        salePrice: res.orderDetail.salePrice,
        orderNumber: res.orderDetail.orderNumber,
        createTime: util.formatTime(res.orderDetail.createTime),
        applicantName: res.insuranceQuotation.applicantName,
        applicantPhoneNumber: res.insuranceQuotation.applicantPhoneNumber,
        applicantIdentityCard: res.insuranceQuotation.applicantIdentityCard,
        insuranceFirstBeneficiary: res.insuranceQuotation.insuranceFirstBeneficiary,
        ifTransferAfterFirstInsure: res.insuranceQuotation.ifTransferAfterFirstInsure === 1 ? '是' : '否',
        insureArea: res.insuranceQuotation.insureArea,
        insuranceQuotationDetail: res.insuranceQuotationDetail,
        orderStatus: res.orderDetail.orderStatus,
        orderStatusName: res.orderDetail.orderStatusName,
        costList: res.CostList,
        returnMoney: res.ReturnMoney,
        remark: res.orderDetail.remark
      });
    });
  },
  /**
   * 点击取消订单
   */
  cancelFn() {
    wx.showModal({
      title: '取消订单',
      content: `确定要取消此保险订单吗？`,
      success: res => {
        if (res.confirm) {
          app.request('/Order/CancelOrder', 'DELETE', {
            userId: app.globalData.userId,
            orderType: 0,
            orderId: this.data.orderId
          }, res => {
            wx.showToast({ title: '取消成功', icon: 'none', duration: 2000 });
            this.updataList();
            wx.navigateBack({ delta: 1 });
          });
        }
      }
    });
  },
  /**
   * 点击删除订单
   */
  deleteFn() {
    wx.showModal({
      title: '删除订单',
      content: `确定要删除此保险订单吗？`,
      success: res => {
        if (res.confirm) {
          app.request('/Order/DeleteOrder', 'DELETE', {
            userId: app.globalData.userId,
            orderType: 0,
            orderId: this.data.orderId
          }, res => {
            wx.showToast({ title: '删除成功', icon: 'none', duration: 2000 });
            this.updataList();
            wx.navigateBack({ delta: 1 });
          });
        }
      }
    });
  },
  /**
   * 点击付款
   */
  payFn() {
    app.request('/WeChat/Unifiedorder', 'POST', {
      userId: app.globalData.userId,
      orderId: this.data.orderId
    }, res => {
      wx.requestPayment({
        ...res,
        success: res => {
          wx.showToast({ title: '支付成功', duration: 2000 });
          this.getData();
          this.updataList();
        },
        fail: res => {
          wx.showToast({ title: '支付失败', icon: 'none', duration: 2000 });
        }
      })
    });
  },
  /**
   * 刷新列表数据
   */
  updataList() {
    var pages = getCurrentPages(); // 当前页面
    var beforePage = pages[pages.length - 2]; // 前一个页面
    beforePage.getData('update');
  }
})